package cn.kaguyaever.web;

import cn.kaguyaever.entity.*;
import cn.kaguyaever.service.CandidateService;
import cn.kaguyaever.service.JudgeService;
import cn.kaguyaever.service.ProjectService;
import cn.kaguyaever.service.VoteService;
import cn.kaguyaever.service.impl.CandidateServiceImpl;
import cn.kaguyaever.service.impl.JudgeServiceImpl;
import cn.kaguyaever.service.impl.ProjectServiceImpl;
import cn.kaguyaever.service.impl.VoteServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/projectServlet")
public class ProjectServlet extends BaseServlet {

    private ProjectService projectService = new ProjectServiceImpl();

    private VoteService voteService = new VoteServiceImpl();

    private JudgeService judgeService = new JudgeServiceImpl();

    private CandidateService candidateService = new CandidateServiceImpl();

    protected void createProject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User loginUser = (User) req.getSession().getAttribute("user");
        Integer userId = loginUser.getId();
        String projectId = System.currentTimeMillis()+""+userId;
        String information = req.getParameter("information");
        String[] judgename = req.getParameterValues("judgename");
        String[] judgepassword = req.getParameterValues("judgepassword");
        if(judgename==null){
            req.setAttribute("msg", "至少一个评委！");
            req.setAttribute("information",information);
            req.getRequestDispatcher("user/create_project.jsp").forward(req, resp);
        }else {
            Project project = new Project(projectId, information, 0, userId);
            projectService.createProject(project);
            int len = judgename.length;
            for (int i = 0; i < len; i++) {
                judgeService.createJudge(new Judge(null, judgename[i], judgepassword[i], 1, projectId));
            }
            req.getSession().setAttribute("project", project);
            resp.sendRedirect("user/home.jsp");
        }
    }

    protected void projectInformation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Project project = (Project) req.getSession().getAttribute("project");
        List<Vote> votes = (List<Vote>)req.getSession().getAttribute("votes");
        if(votes==null){
            resp.sendRedirect("user/project_information.jsp");
        }else {
            Map<String, List<Candidate>> candidates = new HashMap<String, List<Candidate>>();
            for (Vote vote : votes) {
                List<Candidate> candidateList = candidateService.queryCandidate(vote.getVoteId());
                candidates.put(vote.getVoteId(), candidateList);
                if(vote.getVoteStatus()==1){
                    req.getSession().setAttribute("voting",vote);
                }
            }
            req.getSession().setAttribute("candidates", candidates);
            resp.sendRedirect("user/project_information.jsp");
        }
    }

    protected void deleteProject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Project project =(Project)req.getSession().getAttribute("project");
        String projectId = project.getProjectId();
        projectService.deleteProject(projectId);
        if(req.getSession().getAttribute("project")!=null) {
            req.getSession().removeAttribute("project");
        }
        if(req.getSession().getAttribute("votes")!=null) {
            req.getSession().removeAttribute("votes");
        }
        if(req.getSession().getAttribute("voteId")!=null) {
            req.getSession().removeAttribute("voteId");
        }
        if(req.getSession().getAttribute("judges")!=null) {
            req.getSession().removeAttribute("judges");
        }
        if(req.getSession().getAttribute("candidates")!=null){
            req.getSession().removeAttribute("candidates");
        }
        if(req.getSession().getAttribute("voting")!=null){
            req.getSession().removeAttribute("voting");
        }
        if(req.getSession().getAttribute("verification")!=null){
            req.getSession().removeAttribute("verification");
        }
        resp.sendRedirect("user/home.jsp");
    }
}
